<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="chinese">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
      
    
    
      
    
  <script src="//cdn.jsdelivr.net/npm/pace-js@1/pace.min.js"></script>
  <link href="//cdn.jsdelivr.net/npm/pace-js@1/themes/blue/pace-theme-minimal.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="第十九章 枚举类型关键字enmu可以将一组具名（就是具体的意思，翻译某些地方还是挺烂的）的值的有限集合创建为一种新的类型，而这些具体的值可以作为常规的程序组建使用。这是一种非常有用的功能。 19.1 基本enum特性常用枚举方法： （可以用==来比较enum实例） 123456789101112131415161718public class Main &amp;#123;    enum Shrubbe">
<meta name="keywords" content="Java Php hexo Thinking in Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Thinking in Java 第十九章">
<meta property="og:url" content="http://yoursite.com/2020/02/28/Thinking-in-Java-第十九章/index.html">
<meta property="og:site_name" content="坚持就是胜利">
<meta property="og:description" content="第十九章 枚举类型关键字enmu可以将一组具名（就是具体的意思，翻译某些地方还是挺烂的）的值的有限集合创建为一种新的类型，而这些具体的值可以作为常规的程序组建使用。这是一种非常有用的功能。 19.1 基本enum特性常用枚举方法： （可以用==来比较enum实例） 123456789101112131415161718public class Main &amp;#123;    enum Shrubbe">
<meta property="og:locale" content="chinese">
<meta property="og:updated_time" content="2020-03-01T05:24:39.837Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Thinking in Java 第十九章">
<meta name="twitter:description" content="第十九章 枚举类型关键字enmu可以将一组具名（就是具体的意思，翻译某些地方还是挺烂的）的值的有限集合创建为一种新的类型，而这些具体的值可以作为常规的程序组建使用。这是一种非常有用的功能。 19.1 基本enum特性常用枚举方法： （可以用==来比较enum实例） 123456789101112131415161718public class Main &amp;#123;    enum Shrubbe">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":14,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '190coder'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/02/28/Thinking-in-Java-第十九章/">





  <title>Thinking in Java 第十九章 | 坚持就是胜利</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="chinese">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">坚持就是胜利</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/28/Thinking-in-Java-第十九章/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="190coder">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="坚持就是胜利">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Thinking in Java 第十九章</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-02-28T13:57:43+08:00">
                2020-02-28
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="第十九章-枚举类型"><a href="#第十九章-枚举类型" class="headerlink" title="第十九章 枚举类型"></a>第十九章 枚举类型</h1><p>关键字enmu可以将一组具名（就是具体的意思，翻译某些地方还是挺烂的）的值的有限集合创建为一种新的类型，而这些具体的值可以作为常规的程序组建使用。这是一种非常有用的功能。</p>
<h2 id="19-1-基本enum特性"><a href="#19-1-基本enum特性" class="headerlink" title="19.1 基本enum特性"></a>19.1 基本enum特性</h2><p>常用枚举方法： （可以用==来比较enum实例）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line"></span><br><span class="line">    enum Shrubbery &#123;ONE,TWo,Three&#125;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line"></span><br><span class="line">        for(Shrubbery s : Shrubbery.values())&#123;</span><br><span class="line"></span><br><span class="line">            System.out.println(s.ordinal());</span><br><span class="line">            System.out.println(s.getDeclaringClass());</span><br><span class="line">            System.out.println(s.name());</span><br><span class="line">            System.out.println(s == Shrubbery.ONE);</span><br><span class="line">            System.out.println(s.equals(Shrubbery.ONE));</span><br><span class="line">            System.out.println(s.compareTo(Shrubbery.ONE));</span><br><span class="line">            System.out.println(&quot;--------&quot;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还可以用import static 导入枚举类</p>
<h2 id="19-2-向enum中添加新方法"><a href="#19-2-向enum中添加新方法" class="headerlink" title="19.2 向enum中添加新方法"></a>19.2 向enum中添加新方法</h2><p> enum中的构造器与方法和普通的类没有区别，因为除了少许限制（不能继承自一个enum）外，enum就是一个普通的类。一旦enum的定义结束，编译器就不允许我们再使用其构造器创建任何实例。</p>
<p>覆盖enum的方法<br>枚举类也可以重写toString()方法，给我们提供了另一种方式来为枚举实例生成不同的字符串描述信息。</p>
<h2 id="19-3-switch语句中的enum"><a href="#19-3-switch语句中的enum" class="headerlink" title="19.3 switch语句中的enum"></a>19.3 switch语句中的enum</h2><p>Java SE7中switch支持字符串。</p>
<h2 id="19-4-values-的神秘之处"><a href="#19-4-values-的神秘之处" class="headerlink" title="19.4 values()的神秘之处"></a>19.4 values()的神秘之处</h2><p>Enum接口并没有values()方法。<br>values()是由编译器添加的static方法。（反编译后可查看）<br>Class中有一个getEnumConstants()方法，所以即便Enum接口中没有values()方法，我们仍然可以通过Class对象取得所有enum实例。</p>
<h2 id="19-5-实现，而非继承"><a href="#19-5-实现，而非继承" class="headerlink" title="19.5 实现，而非继承"></a>19.5 实现，而非继承</h2><p>enum 类都继承Java.lang.Enum类。而且 不能再继承其它类。但是可以实现一个或者多个接口。<br>enum CartoonCharacter implements Generator<cartooncharacter>{}</cartooncharacter></p>
<h2 id="19-6-随机选取"><a href="#19-6-随机选取" class="headerlink" title="19.6 随机选取"></a>19.6 随机选取</h2><p>使用泛型 random 对 枚举 随机选取。（具体看书中代码） 。</p>
<h2 id="19-7-使用接口组织枚举"><a href="#19-7-使用接口组织枚举" class="headerlink" title="19.7 使用接口组织枚举"></a>19.7 使用接口组织枚举</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Interface Food&#123;</span><br><span class="line">    enum Apptizer implements Food&#123;</span><br><span class="line">        SALAD, SOUP</span><br><span class="line">    &#125;</span><br><span class="line">    enum MainCourse implements Food&#123;</span><br><span class="line">        LASAGEN, BURRITO</span><br><span class="line">    &#125;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由于枚举只能实现接口不能继承，所以遍历枚举需要如上这么写，向上转型 成 Food。</p>
<h2 id="19-8-使用EnumSet替代标志"><a href="#19-8-使用EnumSet替代标志" class="headerlink" title="19.8 使用EnumSet替代标志"></a>19.8 使用EnumSet替代标志</h2><p>EnumSet的设计充分考虑到了速度因素，因为它必须与非常高效的bit标志相竞争。<br>它说明一个二进制位（bit）是否存在时，具有更好的表达能力，并且无需担心性能。<br>EnumSet的基础是long（64个元素）。<br><em>注：EnumSet只是存 状态 0 代表无 1 代表有，真正的信息还是存在枚举里。</em></p>
<h2 id="19-9-使用EnumMap"><a href="#19-9-使用EnumMap" class="headerlink" title="19.9 使用EnumMap"></a>19.9 使用EnumMap</h2><p>EnumMap 底层由数组模式实现，因此实现速度很快，（<strong>这里解释下为什么如果是枚举EnumMap快于hashmap。 1: 不需要计算hashCode()。 2: 只会申请指定枚举的空间大小，没有额外的空间浪费。</strong>）<br>使用命令模式 实现下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">import java.util.EnumMap;</span><br><span class="line">import java.util.*;</span><br><span class="line">interface Command &#123; void action();&#125;</span><br><span class="line">public class EnumMapTest &#123;</span><br><span class="line">    enum AlarmPoints &#123;KITCHEN,BATHROOM,UTILITY&#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        EnumMap&lt;AlarmPoints,Command&gt; em = new EnumMap&lt;&gt;(AlarmPoints.class);</span><br><span class="line"></span><br><span class="line">        em.put(AlarmPoints.KITCHEN, new Command()  &#123;</span><br><span class="line">            public void action() &#123;</span><br><span class="line">                System.out.println(&quot;Kitchen fire!&quot;); &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        em.put(AlarmPoints.BATHROOM, new Command() &#123;</span><br><span class="line">            public void action() &#123; System.out.println(&quot;Bathroom alert!&quot;); &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        for(</span><br><span class="line">                Map.Entry&lt;AlarmPoints,Command&gt; e : em.entrySet()) &#123;</span><br><span class="line">                System.out.println(e.getKey() + &quot;: &quot;);</span><br><span class="line">                e.getValue().action();</span><br><span class="line">        &#125;</span><br><span class="line">        try &#123; // If there’s no value for a particular key:</span><br><span class="line">            em.get(AlarmPoints.UTILITY).action();</span><br><span class="line">        &#125; catch(Exception e) &#123;</span><br><span class="line">            System.out.println(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>与常量相关的方法相比，EnumMap允许改变值对象。</p>
<h2 id="19-10-常量的相关方法"><a href="#19-10-常量的相关方法" class="headerlink" title="19.10 常量的相关方法"></a>19.10 常量的相关方法</h2><p>书中例子为 表驱动法 （解析：从表里面查找信息而不是使用逻辑语句（if…else…switch），当是很简单的情况时，用逻辑语句很简单，但如果逻辑很复杂，再使用逻辑语句就很麻烦了。）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">import java.text.DateFormat;</span><br><span class="line">import java.util.Date;</span><br><span class="line"></span><br><span class="line">public enum ConstantSpecificMethod &#123;</span><br><span class="line">    DATE_TIME &#123;</span><br><span class="line">        String getInfo() &#123;</span><br><span class="line">            return DateFormat.getDateInstance().format(new Date());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    CLASSPATH &#123;</span><br><span class="line">        String getInfo() &#123;</span><br><span class="line">            return System.getenv(&quot;CLASSPATH&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    VERSION &#123;</span><br><span class="line">        String getInfo() &#123;</span><br><span class="line">            return System.getProperty(&quot;java.version&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    abstract String getInfo();</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        for(ConstantSpecificMethod csm : values())</span><br><span class="line">            System.out.println(csm.getInfo());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>与使用匿名内部类相比，定义常量相关方法的语法更高效、简洁。<br>虽然enum有些限制，但一般而言，我们还是可以将其看做是类。</p>
<h4 id="19-10-1-使用enum的职责链"><a href="#19-10-1-使用enum的职责链" class="headerlink" title="19.10.1 使用enum的职责链"></a>19.10.1 使用enum的职责链</h4><p>Enum定义的次序决定了各个解决策略在应用时的次序。（职责链是一种设计模式，类似击鼓传花、部门审批流程。本级处理不了交给上级处理。程序员以多种不同的方式来解决问题，然后将他们链接在一起。当一个请求来临时，她遍历这个链，直到链中的某个解决方法能够处理该请求。搜索 enum 职责链 查看相关代码，书中例子太长了……）</p>
<h4 id="19-10-2-使用enum的状态机"><a href="#19-10-2-使用enum的状态机" class="headerlink" title="19.10.2 使用enum的状态机"></a>19.10.2 使用enum的状态机</h4><p>状态机可以具有有限个特定的状态，它根据输入，从一个状态转移到下一个状态，不过也可能存在瞬时状态，而一旦任务执行结束，状态机就会立刻离开瞬时状态。每个状态都具有某些可接受的输入，不同的输入会使状态机转移到不同的新状态。由于enum对其实例有严格限制，非常适合用来表现不同的状态和输入。</p>
<h2 id="19-11-多路分发"><a href="#19-11-多路分发" class="headerlink" title="19.11 多路分发"></a>19.11 多路分发</h2><h4 id="19-11-1-enum-多路分发"><a href="#19-11-1-enum-多路分发" class="headerlink" title="19.11.1 enum 多路分发"></a>19.11.1 enum 多路分发</h4><p> （挺拧巴的 原理就是按照枚举顺序提前定义好顺序，循环去比，不好理解。实操起来也比较麻烦）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">enum Outcome &#123; WIN, LOSE, DRAW &#125;</span><br><span class="line">public enum RoShamBo2 implements Competitor&lt;RoShamBo2&gt; &#123;</span><br><span class="line">    // 在此处是按顺序定义好，比如 PAPER vs PAPER = DRAW</span><br><span class="line">    ROCK(Outcome.LOSE, Outcome.WIN, Outcome.DRAW),</span><br><span class="line">    PAPER(Outcome.DRAW, Outcome.LOSE, Outcome.WIN),</span><br><span class="line">    SCISSORS(Outcome.WIN, Outcome.DRAW, Outcome.LOSE);</span><br><span class="line">    private Outcome vPAPER, vSCISSORS, vROCK;</span><br><span class="line">    RoShamBo2(Outcome paper,Outcome scissors,Outcome rock) &#123;</span><br><span class="line">        this.vPAPER = paper;</span><br><span class="line">        this.vSCISSORS = scissors;</span><br><span class="line">        this.vROCK = rock;</span><br><span class="line">    &#125;</span><br><span class="line">    public Outcome compete(RoShamBo2 it) &#123;</span><br><span class="line">        switch(it) &#123;</span><br><span class="line">            default:</span><br><span class="line">            case PAPER: return vPAPER;</span><br><span class="line">            case SCISSORS: return vSCISSORS;</span><br><span class="line">            case ROCK: return vROCK;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static &lt;T extends Competitor&lt;T&gt;&gt; void match(T a, T b) &#123;</span><br><span class="line">        System.out.println(a + &quot; vs &quot; + b + &quot;: &quot; + a.compete(b));</span><br><span class="line">    &#125;</span><br><span class="line">    /**</span><br><span class="line">     * 随机产生指定数量个场景（对象）进行游戏</span><br><span class="line">     * 调用了Enums工具方法Enums.random</span><br><span class="line">     * &lt;T extends Enum&lt;T&gt; &amp; Competitor&lt;T&gt;&gt; 规定传入的参数必须同时是Enum&lt;T&gt;和Competitor&lt;T&gt;类型</span><br><span class="line">     * @param rsbClass 用来比赛的对象</span><br><span class="line">     * @param size 数量</span><br><span class="line">     */</span><br><span class="line">    public static &lt;T extends Enum&lt;T&gt; &amp; Competitor&lt;T&gt;&gt;</span><br><span class="line">    void play(Class&lt;T&gt; rsbClass, int size) &#123;</span><br><span class="line">        for(int i = 0; i &lt; size; i++) &#123;</span><br><span class="line">            match(Enums.random(rsbClass), Enums.random(rsbClass));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    public static void main(String[] argv) &#123;</span><br><span class="line">        play(RoShamBo2.class, 20);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="19-11-2-使用常量相关的方法"><a href="#19-11-2-使用常量相关的方法" class="headerlink" title="19.11.2 使用常量相关的方法"></a>19.11.2 使用常量相关的方法</h4><p>常量相关的方法允许我们为每个enum实例提供方法的不同实现，这使得常量相关的方法似乎是实现多路并发的完美解决方案</p>
<h4 id="19-11-3-使用EnumMap-分发"><a href="#19-11-3-使用EnumMap-分发" class="headerlink" title="19.11.3 使用EnumMap 分发"></a>19.11.3 使用EnumMap 分发</h4><p>实现“真正”的两路分发。</p>
<h4 id="19-11-3-使用二维数组"><a href="#19-11-3-使用二维数组" class="headerlink" title="19.11.3 使用二维数组"></a>19.11.3 使用二维数组</h4><p>原理都差不多，定义好类似矩阵顺序，去数组中匹配。</p>
<h2 id="19-12-总结"><a href="#19-12-总结" class="headerlink" title="19.12 总结"></a>19.12 总结</h2><p>枚举本身不复杂， 但是与Java的其他功能结合使用，涉及的功能还是挺细碎繁琐的。尤其是多路分发，需要很好的理解枚举类顺序。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/02/28/Thinking-in-Java-第十八章/" rel="next" title="Thinking in Java 第十八章">
                <i class="fa fa-chevron-left"></i> Thinking in Java 第十八章
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/03/01/Thinking-in-Java-第二十章/" rel="prev" title="Thinking in Java 第二十章">
                Thinking in Java 第二十章 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="190coder">
            
              <p class="site-author-name" itemprop="name">190coder</p>
              <p class="site-description motion-element" itemprop="description">坚信持续学习就一定能超过大多数人</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">53</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          <div style>
  <canvas id="canvas" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
</div>
<script>
(function(){

   var digit=
    [
        [
            [0,0,1,1,1,0,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,0,1,1,0],
            [0,0,1,1,1,0,0]
        ],//0
        [
            [0,0,0,1,1,0,0],
            [0,1,1,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [1,1,1,1,1,1,1]
        ],//1
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,1,1],
            [1,1,1,1,1,1,1]
        ],//2
        [
            [1,1,1,1,1,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//3
        [
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,0],
            [0,0,1,1,1,1,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,1,1,0],
            [1,1,1,1,1,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,1]
        ],//4
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,1,1,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//5
        [
            [0,0,0,0,1,1,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//6
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0]
        ],//7
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//8
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,1,1,0,0,0,0]
        ],//9
        [
            [0,0,0,0,0,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,0,0,0]
        ]//:
    ];

var canvas = document.getElementById('canvas');

if(canvas.getContext){
    var cxt = canvas.getContext('2d');
    //声明canvas的宽高
    var H = 100,W = 700;
    canvas.height = H;
    canvas.width = W;
    cxt.fillStyle = '#f00';
    cxt.fillRect(10,10,50,50);

    //存储时间数据
    var data = [];
    //存储运动的小球
    var balls = [];
    //设置粒子半径
    var R = canvas.height/20-1;
    (function(){
        var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
        //存储时间数字，由十位小时、个位小时、冒号、十位分钟、个位分钟、冒号、十位秒钟、个位秒钟这7个数字组成
        data.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
    })();

    /*生成点阵数字*/
    function renderDigit(index,num){
        for(var i = 0; i < digit[num].length; i++){
            for(var j = 0; j < digit[num][i].length; j++){
                if(digit[num][i][j] == 1){
                    cxt.beginPath();
                    cxt.arc(14*(R+2)*index + j*2*(R+1)+(R+1),i*2*(R+1)+(R+1),R,0,2*Math.PI);
                    cxt.closePath();
                    cxt.fill();
                }
            }
        }
    }

    /*更新时钟*/
    function updateDigitTime(){
        var changeNumArray = [];
        var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
        var NewData = [];
        NewData.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
        for(var i = data.length-1; i >=0 ; i--){
            //时间发生变化
            if(NewData[i] !== data[i]){
                //将变化的数字值和在data数组中的索引存储在changeNumArray数组中
                changeNumArray.push(i+'_'+(Number(data[i])+1)%10);
            }
        }
        //增加小球
        for(var i = 0; i< changeNumArray.length; i++){
            addBalls.apply(this,changeNumArray[i].split('_'));
        }
        data = NewData.concat();
    }

    /*更新小球状态*/
    function updateBalls(){
        for(var i = 0; i < balls.length; i++){
            balls[i].stepY += balls[i].disY;
            balls[i].x += balls[i].stepX;
            balls[i].y += balls[i].stepY;
            if(balls[i].x > W + R || balls[i].y > H + R){
                balls.splice(i,1);
                i--;
            }
        }
    }

    /*增加要运动的小球*/
    function addBalls(index,num){
        var numArray = [1,2,3];
        var colorArray =  ["#3BE","#09C","#A6C","#93C","#9C0","#690","#FB3","#F80","#F44","#C00"];
        for(var i = 0; i < digit[num].length; i++){
            for(var j = 0; j < digit[num][i].length; j++){
                if(digit[num][i][j] == 1){
                    var ball = {
                        x:14*(R+2)*index + j*2*(R+1)+(R+1),
                        y:i*2*(R+1)+(R+1),
                        stepX:Math.floor(Math.random() * 4 -2),
                        stepY:-2*numArray[Math.floor(Math.random()*numArray.length)],
                        color:colorArray[Math.floor(Math.random()*colorArray.length)],
                        disY:1
                    };
                    balls.push(ball);
                }
            }
        }
    }

    /*渲染*/
    function render(){
        //重置画布宽度，达到清空画布的效果
        canvas.height = 100;
        //渲染时钟
        for(var i = 0; i < data.length; i++){
            renderDigit(i,data[i]);
        }
        //渲染小球
        for(var i = 0; i < balls.length; i++){
            cxt.beginPath();
            cxt.arc(balls[i].x,balls[i].y,R,0,2*Math.PI);
            cxt.fillStyle = balls[i].color;
            cxt.closePath();
            cxt.fill();
        }
    }

    clearInterval(oTimer);
    var oTimer = setInterval(function(){
        //更新时钟
        updateDigitTime();
        //更新小球状态
        updateBalls();
        //渲染
        render();
    },50);
}

})();
</script>



        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#第十九章-枚举类型"><span class="nav-number">1.</span> <span class="nav-text">第十九章 枚举类型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#19-1-基本enum特性"><span class="nav-number">1.1.</span> <span class="nav-text">19.1 基本enum特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-2-向enum中添加新方法"><span class="nav-number">1.2.</span> <span class="nav-text">19.2 向enum中添加新方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-3-switch语句中的enum"><span class="nav-number">1.3.</span> <span class="nav-text">19.3 switch语句中的enum</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-4-values-的神秘之处"><span class="nav-number">1.4.</span> <span class="nav-text">19.4 values()的神秘之处</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-5-实现，而非继承"><span class="nav-number">1.5.</span> <span class="nav-text">19.5 实现，而非继承</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-6-随机选取"><span class="nav-number">1.6.</span> <span class="nav-text">19.6 随机选取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-7-使用接口组织枚举"><span class="nav-number">1.7.</span> <span class="nav-text">19.7 使用接口组织枚举</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-8-使用EnumSet替代标志"><span class="nav-number">1.8.</span> <span class="nav-text">19.8 使用EnumSet替代标志</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-9-使用EnumMap"><span class="nav-number">1.9.</span> <span class="nav-text">19.9 使用EnumMap</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-10-常量的相关方法"><span class="nav-number">1.10.</span> <span class="nav-text">19.10 常量的相关方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#19-10-1-使用enum的职责链"><span class="nav-number">1.10.0.1.</span> <span class="nav-text">19.10.1 使用enum的职责链</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#19-10-2-使用enum的状态机"><span class="nav-number">1.10.0.2.</span> <span class="nav-text">19.10.2 使用enum的状态机</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-11-多路分发"><span class="nav-number">1.11.</span> <span class="nav-text">19.11 多路分发</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#19-11-1-enum-多路分发"><span class="nav-number">1.11.0.1.</span> <span class="nav-text">19.11.1 enum 多路分发</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#19-11-2-使用常量相关的方法"><span class="nav-number">1.11.0.2.</span> <span class="nav-text">19.11.2 使用常量相关的方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#19-11-3-使用EnumMap-分发"><span class="nav-number">1.11.0.3.</span> <span class="nav-text">19.11.3 使用EnumMap 分发</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#19-11-3-使用二维数组"><span class="nav-number">1.11.0.4.</span> <span class="nav-text">19.11.3 使用二维数组</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#19-12-总结"><span class="nav-number">1.12.</span> <span class="nav-text">19.12 总结</span></a></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

	
           
         
         <div style>
  <canvas id="canvas" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
</div>
<script>
(function(){

   var digit=
    [
        [
            [0,0,1,1,1,0,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,0,1,1,0],
            [0,0,1,1,1,0,0]
        ],//0
        [
            [0,0,0,1,1,0,0],
            [0,1,1,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [1,1,1,1,1,1,1]
        ],//1
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,1,1],
            [1,1,1,1,1,1,1]
        ],//2
        [
            [1,1,1,1,1,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//3
        [
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,0],
            [0,0,1,1,1,1,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,1,1,0],
            [1,1,1,1,1,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,1]
        ],//4
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,1,1,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//5
        [
            [0,0,0,0,1,1,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//6
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0]
        ],//7
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//8
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,1,1,0,0,0,0]
        ],//9
        [
            [0,0,0,0,0,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,0,0,0]
        ]//:
    ];

var canvas = document.getElementById('canvas');

if(canvas.getContext){
    var cxt = canvas.getContext('2d');
    //声明canvas的宽高
    var H = 100,W = 700;
    canvas.height = H;
    canvas.width = W;
    cxt.fillStyle = '#f00';
    cxt.fillRect(10,10,50,50);

    //存储时间数据
    var data = [];
    //存储运动的小球
    var balls = [];
    //设置粒子半径
    var R = canvas.height/20-1;
    (function(){
        var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
        //存储时间数字，由十位小时、个位小时、冒号、十位分钟、个位分钟、冒号、十位秒钟、个位秒钟这7个数字组成
        data.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
    })();

    /*生成点阵数字*/
    function renderDigit(index,num){
        for(var i = 0; i < digit[num].length; i++){
            for(var j = 0; j < digit[num][i].length; j++){
                if(digit[num][i][j] == 1){
                    cxt.beginPath();
                    cxt.arc(14*(R+2)*index + j*2*(R+1)+(R+1),i*2*(R+1)+(R+1),R,0,2*Math.PI);
                    cxt.closePath();
                    cxt.fill();
                }
            }
        }
    }

    /*更新时钟*/
    function updateDigitTime(){
        var changeNumArray = [];
        var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
        var NewData = [];
        NewData.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
        for(var i = data.length-1; i >=0 ; i--){
            //时间发生变化
            if(NewData[i] !== data[i]){
                //将变化的数字值和在data数组中的索引存储在changeNumArray数组中
                changeNumArray.push(i+'_'+(Number(data[i])+1)%10);
            }
        }
        //增加小球
        for(var i = 0; i< changeNumArray.length; i++){
            addBalls.apply(this,changeNumArray[i].split('_'));
        }
        data = NewData.concat();
    }

    /*更新小球状态*/
    function updateBalls(){
        for(var i = 0; i < balls.length; i++){
            balls[i].stepY += balls[i].disY;
            balls[i].x += balls[i].stepX;
            balls[i].y += balls[i].stepY;
            if(balls[i].x > W + R || balls[i].y > H + R){
                balls.splice(i,1);
                i--;
            }
        }
    }

    /*增加要运动的小球*/
    function addBalls(index,num){
        var numArray = [1,2,3];
        var colorArray =  ["#3BE","#09C","#A6C","#93C","#9C0","#690","#FB3","#F80","#F44","#C00"];
        for(var i = 0; i < digit[num].length; i++){
            for(var j = 0; j < digit[num][i].length; j++){
                if(digit[num][i][j] == 1){
                    var ball = {
                        x:14*(R+2)*index + j*2*(R+1)+(R+1),
                        y:i*2*(R+1)+(R+1),
                        stepX:Math.floor(Math.random() * 4 -2),
                        stepY:-2*numArray[Math.floor(Math.random()*numArray.length)],
                        color:colorArray[Math.floor(Math.random()*colorArray.length)],
                        disY:1
                    };
                    balls.push(ball);
                }
            }
        }
    }

    /*渲染*/
    function render(){
        //重置画布宽度，达到清空画布的效果
        canvas.height = 100;
        //渲染时钟
        for(var i = 0; i < data.length; i++){
            renderDigit(i,data[i]);
        }
        //渲染小球
        for(var i = 0; i < balls.length; i++){
            cxt.beginPath();
            cxt.arc(balls[i].x,balls[i].y,R,0,2*Math.PI);
            cxt.fillStyle = balls[i].color;
            cxt.closePath();
            cxt.fill();
        }
    }

    clearInterval(oTimer);
    var oTimer = setInterval(function(){
        //更新时钟
        updateDigitTime();
        //更新小球状态
        updateBalls();
        //渲染
        render();
    },50);
}

})();
</script>



    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">190coder</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>





<div>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>
    <span id="busuanzi_container_site_uv">本站访客数<span id="busuanzi_value_site_uv"></span>人</span>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
